package com.example.mylibrarybase.yumengmeng04.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.example.mylibrarybase.R;
import com.example.mylibrarybase.bar.initAction_Bar;
import com.example.mylibrarybase.base.baseui.BaseActivity;
import com.example.mylibrarybase.net.netWork;
import com.example.mylibrarybase.utils.LogUtils;
import com.example.mylibrarybase.utils.UtilsToast;
import com.example.mylibrarybase.yumengmeng03.view.TopTabBar_3;
import com.example.mylibrarybase.yumengmeng04.adapter.MyListView_04_more;
import com.example.mylibrarybase.yumengmeng04.base.BaseListViewAdapter_04;
import com.example.mylibrarybase.yumengmeng04.bean.oder_04;
import com.example.mylibrarybase.yumengmeng04.view.listViewlist_04;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lzy.okgo.cache.CacheMode;

import java.util.ArrayList;


public class MainActivity_04_listmore extends BaseActivity implements TopTabBar_3.CallBack {
    ArrayList<oder_04.ResultBean> mOders = new ArrayList<>();
    private TopTabBar_3 mTopTabBar;
    private listViewlist_04 mRoot_man_listView;
    int type;
    private PullToRefreshLayout ptrl;
    private initAction_Bar mBase_action_bar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    public int getlayouXML() {
        return R.layout.mianactivity_listsingle;
    }

    @Override
    public void initView() {
        ptrl = ((PullToRefreshLayout) findViewById(R.id.refresh_view1));
        ptrl.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
                // 下拉刷新操作
                new Handler()
                {
                    @Override
                    public void handleMessage(Message msg)
                    {
                        // 千万别忘了告诉控件刷新完毕了哦！
                        pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                    }
                }.sendEmptyMessageDelayed(0, 2000);
            }

            @Override
            public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
                // 下拉刷新操作
                new Handler()
                {
                    @Override
                    public void handleMessage(Message msg)
                    {
                        // 千万别忘了告诉控件刷新完毕了哦！
                        pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                    }
                }.sendEmptyMessageDelayed(0, 2000);
            }
        });
        mTopTabBar = (TopTabBar_3) findViewById(R.id.mianactivity_listsingletop_tab_bar_03);
        mTopTabBar.setCallBack(this);


        mRoot_man_listView = (listViewlist_04) findViewById(R.id.mianactivity_listsingle_listView);

        mBase_action_bar = (initAction_Bar) findViewById(R.id.base_action_bar);
        mBase_action_bar
                .setMode(initAction_Bar.AUTO_ONCLICK_MODE)
                .setCallBack(new initAction_Bar.Action_bar_call_back() {
                    @Override
                    public void getAction_barView_backbutton(TextView textView) {
                        textView.setText("返回");
                    }

                    @Override
                    public void getAction_barView_backbutton_icon(ImageView imageView) {

                    }

                    @Override
                    public void getAction_barView_title(TextView textView) {
                        textView.setText("listview");
                    }

                    @Override
                    public void getAction_barView_right_icon(ImageView imageView) {

                    }
                });


        initData();
    }

    private void setLoadMoreData(int type) {
        switch (type){
//                case :
//
//                break;
        }
    }

    @Override
    public void initData() {
        //String url ="http://app.beta.9ac.com.cn/page/enquiry/biddingLst";
        // String url ="http://app.beta.9ac.com.cn/page/login/table";
        //String url ="http://192.168.1.102:8080/json.txt";
        netWork.goPost(this, netWork.testUrl, null, "1", CacheMode.NO_CACHE, new netWork.CallBack() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                LogUtils.i("回来的成功"+jsonObject);
                try {
                    oder_04 data = jsonObject.getObject("data", oder_04.class);
                    //  oder_04 data = JSONObject.parseObject("data", oder_04.class);
                    LogUtils.i("封装好的bean"+data);
                    if (null!=data) {
                        mOders.addAll(data.result);
                        LogUtils.i("集合内容是" + mOders);
                        setAdapter();
                    }

                }catch (JSONException e){
                    System.out.println(e);
                    UtilsToast.showToast(MainActivity_04_listmore.this, "数据出错");
                }





            }

            @Override
            public void onError(String error) {
                LogUtils.i("回来的失败"+error);
            }
        });

//        netWork.goPost(this, netWork.testUrl0, null, "0", CacheMode.NO_CACHE, new netWork.CallBack() {
//            @Override
//            public void onSuccess(JSONObject jsonObject) {
//                LogUtils.i("我们测试的0开头的json"+jsonObject);
//                String string = jsonObject.getString("0");
//                String string2 = jsonObject.getString("2");
//                LogUtils.i("我们测试的0开头的json里面的内容"+"\n"+"0里面的"+string+"\n"+"2里面的"+string2);
//            }
//
//            @Override
//            public void onError(String error) {
//
//            }
//        });

    }

    private void setAdapter() {
        type = 6;
        MyListView_04_more myListView = new MyListView_04_more(this,type , mOders, new BaseListViewAdapter_04.CallBack() {
            @Override
            public void loadMore(View view, int position, int type) {
                LogUtils.i("显示加载更多了"+"目前的号码是"+position+"加载更多的状态是"+type);
                //view.setVisibility(View.GONE);
                view.setPadding(0,0,0,0);
                view.setVisibility(View.GONE);
                setLoadMoreData(type);
            }
        });
        mRoot_man_listView.setAdapter(myListView);
        mRoot_man_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                LogUtils.i("我点的是"+i);
            }
        });
    }

    @Override
    public void onClickItemNum(int i) {
        LogUtils.i("目前的号是"+i);
    }
}
